/*
 * Jenny Ruppen 09-730-391
 * Sebastien Goldberg 08-914-855
 * Henry Raymond 08-928-061
 * Stefan Mussato 09-752-270
 */
 
package ch.uzh.ifi.sopra.gruppe9.game;

import com.jme3.scene.Node;


/**
 * Draws a selection option button in the GUI.
 */
public class GuiSelectionOption extends Node implements IMouseOverable, IClickable {
	
	
	/**
	 * Selection element to which this option belongs to.
	 */
	private GuiSelection parentSelection;
	
	
	/**
	 * Offset of the coordinate system of this element.
	 */
	private GUI.Offset offset;
	
	
	/**
	 * Position of the element in the x axis. (Assuming the window has a width of 1024 pixel.)
	 */
	private int positionX;
	
	
	/**
	 * Position of the element in the y axis. (Assuming the window has a height of 768 pixel.)
	 */
	private int positionY;
	
	
	/**
	 * Width of the element. (Assuming the window has a width of 1024 pixel.)
	 */
	private int width;
	
	
	/**
	 * Height of the element. (Assuming the window has a height of 768 pixel.)
	 */
	private int height;
	
	
	/**
	 * Z-Index of the element. An element with a higher index will cover an element with a lower one.
	 */
	private int zIndex;
	
	
	/**
	 * Node of the geometry of the selection option button.
	 */
	private Node button;
	
	
	/**
	 * Value of the selection if this button is selected.
	 */
	private int value;
	
	
	/**
	 * True if the value of this option is selected in the selection.
	 */
	private boolean selected;
	
	
	/**
	 * True if this option is active.
	 */
	private boolean active = true;
	
	
	/**
	 * Elements of the selection option button.
	 */
	private GuiQuad cornerTopLeft;
	private GuiQuad cornerTopRight;
	private GuiQuad cornerBottomLeft;
	private GuiQuad cornerBottomRight;
	private GuiQuad lineTop;
	private GuiQuad lineBottom;
	private GuiQuad lineLeft;
	private GuiQuad lineRight;
	private GuiQuad background;
	
	
	/**
	 * Constructor
	 */
	public GuiSelectionOption(GuiSelection selection, int value, GUI.Offset offset, int positionX, int positionY, int width, int height, int zIndex) {
		this.parentSelection = selection;
		this.value = value;
		this.offset = offset;
		this.positionX = positionX;
		this.positionY = positionY;
		this.width = width;
		this.height = height;
		this.zIndex = zIndex;
		
		selected = false;
		
		button = new Node();
		
		cornerTopLeft = new GuiQuad();
		cornerTopLeft.setAbsolutePositionSize(-10, -10, 20, 20, 0);
		cornerTopLeft.setMaterial(MaterialManager.getSelectionOption(), 0.0f, 0.0f, 0.25f, 0.0625f);
		button.attachChild(cornerTopLeft.create());
		
		cornerTopRight = new GuiQuad();
		cornerTopRight.setAbsolutePositionSize(width - 10, -10, 20, 20, 0);
		cornerTopRight.setMaterial(MaterialManager.getSelectionOption(), 0.75f, 0.0f, 0.25f, 0.0625f);
		button.attachChild(cornerTopRight.create());
		
		cornerBottomLeft = new GuiQuad();
		cornerBottomLeft.setAbsolutePositionSize(-10, height - 10, 20, 20, 0);
		cornerBottomLeft.setMaterial(MaterialManager.getSelectionOption(), 0.0f, 0.1875f, 0.25f, 0.0625f);
		button.attachChild(cornerBottomLeft.create());
		
		cornerBottomRight = new GuiQuad();
		cornerBottomRight.setAbsolutePositionSize(width - 10, height - 10, 20, 20, 0);
		cornerBottomRight.setMaterial(MaterialManager.getSelectionOption(), 0.75f, 0.1875f, 0.25f, 0.0625f);
		button.attachChild(cornerBottomRight.create());
		
		lineTop = new GuiQuad();
		lineTop.setAbsolutePositionSize(10, -10, width - 20, 20, 0);
		lineTop.setMaterial(MaterialManager.getSelectionOption(), 0.25f, 0.0f, 0.25f, 0.0625f);
		button.attachChild(lineTop.create());
		
		lineBottom = new GuiQuad();
		lineBottom.setAbsolutePositionSize(10, height - 10, width - 20, 20, 0);
		lineBottom.setMaterial(MaterialManager.getSelectionOption(), 0.25f, 0.1875f, 0.25f, 0.0625f);
		button.attachChild(lineBottom.create());
		
		lineLeft = new GuiQuad();
		lineLeft.setAbsolutePositionSize(-10, 10, 20, height - 20, 0);
		lineLeft.setMaterial(MaterialManager.getSelectionOption(), 0.0f, 0.0625f, 0.25f, 0.0625f);
		button.attachChild(lineLeft.create());
		
		lineRight = new GuiQuad();
		lineRight.setAbsolutePositionSize(width - 10, 10, 20, height - 20, 0);
		lineRight.setMaterial(MaterialManager.getSelectionOption(), 0.75f, 0.0625f, 0.25f, 0.0625f);
		button.attachChild(lineRight.create());
		
		background = new GuiQuad();
		background.setAbsolutePositionSize(10, 10, width - 20, height - 20, -0.5f);
		background.setMaterial(MaterialManager.getSelectionOption(), 0.25f, 0.0625f, 0.25f, 0.0625f);
		button.attachChild(background.create());
		
		button.setLocalTranslation(GUI.calculateX(offset, positionX), GUI.calculateY(offset, positionY), (float)zIndex);
		
        this.attachChild(button);

	}
	
	
	/**
	 * Changes textures to show default button.
	 */
	private void showMouseOutTextures() {
		cornerTopLeft.changeTextureCoords(0.0f, 0.0f, 0.25f, 0.0625f);
		cornerTopRight.changeTextureCoords(0.75f, 0.0f, 0.25f, 0.0625f);
		cornerBottomLeft.changeTextureCoords(0.0f, 0.1875f, 0.25f, 0.0625f);
		cornerBottomRight.changeTextureCoords(0.75f, 0.1875f, 0.25f, 0.0625f);
		lineTop.changeTextureCoords(0.25f, 0.0f, 0.25f, 0.0625f);
		lineBottom.changeTextureCoords(0.25f, 0.1875f, 0.25f, 0.0625f);
		lineLeft.changeTextureCoords(0.0f, 0.0625f, 0.25f, 0.0625f);
		lineRight.changeTextureCoords(0.75f, 0.0625f, 0.25f, 0.0625f);
		background.changeTextureCoords(0.25f, 0.0625f, 0.25f, 0.0625f);
	}
	
	
	/**
	 * Changes textures to show mouseOver button.
	 */
	private void showMouseOverTextures() {
		cornerTopLeft.changeTextureCoords(0.0f, 0.25f, 0.25f, 0.0625f);
		cornerTopRight.changeTextureCoords(0.75f, 0.25f, 0.25f, 0.0625f);
		cornerBottomLeft.changeTextureCoords(0.0f, 0.4375f, 0.25f, 0.0625f);
		cornerBottomRight.changeTextureCoords(0.75f, 0.4375f, 0.25f, 0.0625f);
		lineTop.changeTextureCoords(0.25f, 0.25f, 0.25f, 0.0625f);
		lineBottom.changeTextureCoords(0.25f, 0.4375f, 0.25f, 0.0625f);
		lineLeft.changeTextureCoords(0.0f, 0.3125f, 0.25f, 0.0625f);
		lineRight.changeTextureCoords(0.75f, 0.3125f, 0.25f, 0.0625f);
		background.changeTextureCoords(0.25f, 0.3125f, 0.25f, 0.0625f);
	}
	
	
	/**
	 * Changes textures to show clicked button.
	 */
	private void showSelectedTextures() {
		cornerTopLeft.changeTextureCoords(0.0f, 0.5f, 0.25f, 0.0625f);
		cornerTopRight.changeTextureCoords(0.75f, 0.5f, 0.25f, 0.0625f);
		cornerBottomLeft.changeTextureCoords(0.0f, 0.6875f, 0.25f, 0.0625f);
		cornerBottomRight.changeTextureCoords(0.75f, 0.6875f, 0.25f, 0.0625f);
		lineTop.changeTextureCoords(0.25f, 0.5f, 0.25f, 0.0625f);
		lineBottom.changeTextureCoords(0.25f, 0.6875f, 0.25f, 0.0625f);
		lineLeft.changeTextureCoords(0.0f, 0.5625f, 0.25f, 0.0625f);
		lineRight.changeTextureCoords(0.75f, 0.5625f, 0.25f, 0.0625f);
		background.changeTextureCoords(0.25f, 0.5625f, 0.25f, 0.0625f);
	}
	
	
	/**
	 * Changes textures to show inactive button.
	 */
	private void showInactiveTextures() {
		cornerTopLeft.changeTextureCoords(0.0f, 0.75f, 0.25f, 0.0625f);
		cornerTopRight.changeTextureCoords(0.75f, 0.75f, 0.25f, 0.0625f);
		cornerBottomLeft.changeTextureCoords(0.0f, 0.9375f, 0.25f, 0.0625f);
		cornerBottomRight.changeTextureCoords(0.75f, 0.9375f, 0.25f, 0.0625f);
		lineTop.changeTextureCoords(0.25f, 0.75f, 0.25f, 0.0625f);
		lineBottom.changeTextureCoords(0.25f, 0.9375f, 0.25f, 0.0625f);
		lineLeft.changeTextureCoords(0.0f, 0.8125f, 0.25f, 0.0625f);
		lineRight.changeTextureCoords(0.75f, 0.8125f, 0.25f, 0.0625f);
		background.changeTextureCoords(0.25f, 0.8125f, 0.25f, 0.0625f);
	}
	
	
	/**
	 * Marks option as selected within the selection.
	 */
	public void setSelected(boolean selected) {
		this.selected = selected;
		if(selected) {
			showSelectedTextures();
		} else {
			showMouseOutTextures();
		}
	}
	
	
	/**
	 * Deactivates option.
	 */
	public void deactivate() {
		active = false;
		showInactiveTextures();
	}
	
	
	/**
	 * Activates option.
	 */
	public void activate() {
		active = true;
		showMouseOutTextures();
	}
	
	
	@Override
	public GUI.Offset getOffset() {
		return offset;
	}


	@Override
	public int getPositionX() {
		return positionX;
	}


	@Override
	public int getPositionY() {
		return positionY;
	}


	@Override
	public int getWidth() {
		return width;
	}


	@Override
	public int getHeight() {
		return height;
	}


	@Override
	public void onMouseOver() {
		if(!selected && active) {
			showMouseOverTextures();
		}
	}


	@Override
	public void onMouseOut() {
		if(!selected && active) {
			showMouseOutTextures();
		}
	}
	
	
	@Override
	public void onMouseDown(int mouseX, int mouseY) {
		if(active) {
			showSelectedTextures();
		}
	}


	@Override
	public void onMouseUp(int mouseX, int mouseY) {
		if(active) {
			if(selected) {
				parentSelection.removeValue(value);
			} else {
				parentSelection.addValue(value);
			}
		}
	}


	public int getZIndex() {
		return zIndex;
	}


	public int getValue() {
		return value;
	}
	
}
